package com.cwj;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.poi.ss.formula.functions.T;

import java.lang.reflect.ParameterizedType;
import java.util.*;
import java.util.regex.Pattern;

public class Test01 {
    public static void main(String[] args) throws JsonProcessingException, InterruptedException {
        int[] nums = new int[10];
        int length = nums.length;
        int hashLength = tableSizeFor(length);

        double v = Double.parseDouble("123.456");
        System.out.println(maximum(1, 2));
        System.out.println(maximum(-1, 2));
        System.out.println(maximum(1, -2));
        System.out.println(maximum(-1, -2));
        System.out.println("-------------");
        System.out.println(maximum(2, 1));
        System.out.println(maximum(-2, 1));
        System.out.println(maximum(2, -1));
        System.out.println(maximum(-2, -1));
        int i = Integer.bitCount(0b10010);
        System.out.println(i);
        int i1 = Integer.highestOneBit(0b1011);
        System.out.println(Integer.toBinaryString(i1));
        int bitCount2 = 0;
        int num = 0;
        if(num != 0){
            if(num >> 16 != 0){bitCount2 += 16; num >>= 16;}
            if(num >> 8 !=0){bitCount2 += 8; num >>= 8;}
            if(num >> 4 !=0){bitCount2 += 4; num >>= 4;}
            if(num >> 2 !=0){bitCount2 += 2; num >>= 2;}
            if(num >> 1 !=0){bitCount2 += 1; num >>= 1;}
        }
        System.out.println(bitCount2);
        int m = 0b10010;
        int n = 0b10010;
        m &= m - 5;
        n &= (n - 5);
        System.out.println(Integer.toBinaryString(m));
        System.out.println(Integer.toBinaryString(n));
        System.out.println(Integer.toBinaryString(Integer.lowestOneBit(0b10100)));
        System.out.println(Integer.toBinaryString(Integer.highestOneBit(0b10100)));
        System.out.println(0b101100111);
        System.out.println(0b101101101);
        System.out.println(Integer.toBinaryString(643));
        System.out.println(Integer.toBinaryString(616));
        System.out.println(0b1111_1111_1111_1111_1111_1111_1111_0100);
        System.out.println(0b1111_1111_1111_1111_1111_1111_1111_0110);
        System.out.println(Integer.MIN_VALUE / -1);
        System.out.println(Integer.MIN_VALUE);
        System.out.println(Integer.MAX_VALUE / -1);
        System.out.println(Integer.MAX_VALUE);
        System.out.println(-1 / Integer.MIN_VALUE);
        System.out.println(Integer.MIN_VALUE);
        System.out.println(-1 / Integer.MAX_VALUE);
        System.out.println(Integer.MAX_VALUE);

        test01();

        genericMethod();

        List<Integer> list1 = new ArrayList<>();
        List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
        integers.parallelStream().forEach(
            e -> {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException interruptedException) {
                    interruptedException.printStackTrace();
                }
                list1.add(e);
            }
        );
        System.out.println("listSize:" + list1.size() + " : " + list1.toString());

        testRegix();
    }

    public static void test01() {
        int[] arr = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        System.out.println(Arrays.toString(arr));
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            int r = random.nextInt(arr.length);
            if(i != r){
                arr[i] = arr[i] ^ arr[r];
                arr[r] = arr[i] ^ arr[r];
                arr[i] = arr[i] ^ arr[r];
            }
        }
        System.out.println();
        System.out.println(Arrays.toString(arr));
        System.out.println(UUID.randomUUID().toString());

        boolean matches = Pattern.matches("^1212$", "");
        System.out.println(matches);

    }
    public static int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();
        for(int n : nums1){
            set1.add(n);
        }
        for(int n : nums1){
            set2.add(n);
        }
        if(set1.size() == 0 || set2.size() ==  0){
            return new int[0];
        }
        Iterator<Integer> it = set1.iterator();
        while(it.hasNext()){
            Integer n = it.next();
            if(!set2.contains(n)){
                it.remove();
            }
        }
        return set1.stream().mapToInt(Integer::intValue).toArray();

    }

    /**
     *
     * @param a
     * @param b
     * @return
     */
    public static int maximum(int a, int b) {
        int k = (b - a) >>> 31;
        return a * k | b * (k ^ 1);
    }

    public static void genericMethod() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> map = new HashMap<>();
        map.put("name", "tom");
        map.put("age", 15);
        String s = objectMapper.writeValueAsString(map);
        System.out.println(s);

        Map<String, Object> map1 = objectMapper.readValue(s, Map.class);
        System.out.println(map1);

    }

    static final int tableSizeFor(int cap) {
        int n = cap - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
    }
    static final int MAXIMUM_CAPACITY = 1 << 30;

    static void testRegix(){
        String s = "abc.jpg";
        String s1 = s.replaceFirst("[a-z0-9]+\\.jpg", "");
        System.out.println(s1);
    }

}
